home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 2001 May / SGI Freeware 2001 May - Disc 1.iso / dist / fw_mysql.idb / usr / freeware / share / sql-bench / test-connect.z / test-connect
Encoding:
Text File  |  1999-10-18  |  6.9 KB  |  256 lines

  1. #!/bin/perl5
  2. # This test is for testing the speed of connections and sending
  3. # data to the client.
  4. #
  5. # By changing the variable '$opt_loop_count' value you can make this test
  6. # easier/harderto your computer to execute. You can also change this value
  7. # by using option --loop_value='what_ever_you_like'.
  8. ##################### Standard benchmark inits ##############################
  9.  
  10. use DBI;
  11. use Benchmark;
  12.  
  13. $opt_loop_count=10000;    # Change this to make test harder/easier
  14. $str_length=65000;    # This is the length of blob strings in PART:5
  15. $max_test=100;        # How many times to test if the server is busy
  16.  
  17. chomp($pwd = `pwd`); $pwd = "." if ($pwd eq '');
  18. require "$pwd/bench-init.pl" || die "Can't read Configuration file: $!\n";
  19.  
  20. # This is the length of blob strings in PART:5
  21. $str_length=min($limits->{'max_text_size'},$limits->{'query_size'}-30,$str_length);
  22.  
  23. if ($opt_small_test)
  24. {
  25.   $opt_loop_count/=100;
  26. }
  27.  
  28. print "Testing the speed of connecting to the server and sending of data\n";
  29. print "All tests are done $opt_loop_count times\n\n";
  30.  
  31. ################################# PART:1 ###################################
  32. ####
  33. ####  Start timeing and start connect test..
  34. ####
  35.  
  36. $start_time=new Benchmark;
  37.  
  38. print "Testing connection/disconnect\n";
  39.  
  40. $loop_time=new Benchmark;
  41. $errors=0;
  42.  
  43. for ($i=0 ; $i < $opt_loop_count ; $i++)
  44. {
  45.   print "$i " if (($opt_debug));
  46.   for ($j=0; $j < $max_test ; $j++)
  47.   {
  48.     if ($dbh = DBI->connect($server->{'data_source'}, $opt_user,
  49.                 $opt_password))
  50.     {
  51.       $dbh->disconnect;
  52.       last;
  53.     }
  54.     select(undef, undef, undef, 0.001);
  55.     print "$errors " if (($opt_debug));
  56.     $errors++;
  57.   }
  58.   die $DBI::errstr if ($j == $max_test);
  59.   $dbh->disconnect;
  60.   undef($dbh);
  61. }
  62. $end_time=new Benchmark;
  63. print "Warning: $errors connections didn't work without a time delay\n" if ($errors);
  64. print "Time to connect ($opt_loop_count): " .
  65.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  66.  
  67. ################################# PART:2 ###################################
  68. #### Now we shall do first one connect, then simple select
  69. #### (select 1..) and then close connection. This will be
  70. #### done $opt_loop_count times.
  71.  
  72. if ($limits->{'select_without_from'})
  73. {
  74.   print "Test connect/simple select/disconnect\n";
  75.   $loop_time=new Benchmark;
  76.  
  77.   for ($i=0; $i<$opt_loop_count; $i++)
  78.   {
  79.     $dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password) || die $DBI::errstr;
  80.     $sth = $dbh->do("select 1") or die $DBI::errstr;
  81.     $dbh->disconnect;
  82.   }
  83.   $end_time=new Benchmark;
  84.   print "Time for select_simple ($opt_loop_count): " .
  85.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  86. }
  87.  
  88. ################################# PART:3 ###################################
  89. ####
  90. #### Okay..Next thing we'll do is a simple select $opt_loop_count times.
  91. ####
  92.  
  93. $dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password,
  94.             { PrintError => 0}) || die $DBI::errstr;
  95.  
  96. if ($limits->{'select_without_from'})
  97. {
  98.   print "Test simple select\n";
  99.   $loop_time=new Benchmark;
  100.   for ($i=0 ; $i<$opt_loop_count ; $i++)
  101.   {
  102.     $sth = $dbh->do("select 1") or die $DBI::errstr;
  103.   }
  104.   $end_time=new Benchmark;
  105.   print "Time for select_simple ($opt_loop_count): " .
  106.     timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  107. }
  108.  
  109. ################################# PART:4 ###################################
  110. #### First, we'll create a simple table 'bench1'
  111. #### Then we shall do $opt_loop_count selects from this table.
  112. #### Table will contain very simple data.
  113.  
  114. $sth = $dbh->do("drop table bench1");
  115. do_many($dbh,$server->create("bench1",
  116.                  ["a int NOT NULL",
  117.                   "i int",
  118.                   "s char(10)"],
  119.                  ["primary key (a)"]));
  120. $sth = $dbh->do("insert into bench1 values(1,100,'AAA')") or die $DBI::errstr;
  121.  
  122. if ($opt_fast && $opt_server eq "pg")
  123. {
  124.   $server->vacuum($dbh);
  125. }
  126. $dbh->disconnect;
  127.  
  128. #
  129. # First test connect/select/disconnect
  130. #
  131. print "Testing connect/select 1 row from table/disconnect\n";
  132.  
  133. $loop_time=new Benchmark;
  134. $errors=0;
  135.  
  136. for ($i=0 ; $i<$opt_loop_count ; $i++)
  137. {
  138.   for ($j=0; $j < $max_test ; $j++)
  139.   {
  140.     last if ($dbh = DBI->connect($server->{'data_source'}, $opt_user, $opt_password));
  141.     $errors++;
  142.   }
  143.   die $DBI::errstr if ($j == $max_test);
  144.  
  145.   $sth = $dbh->do("select * from bench1") #Select * from table with 1 record
  146.     or die $DBI::errstr;
  147.   $dbh->disconnect;
  148. }
  149.  
  150. $end_time=new Benchmark;
  151. print "Warning: $errors connections didn't work without a time delay\n" if ($errors);
  152. print "Time to connect+select ($opt_loop_count): " .
  153.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  154.  
  155. #
  156. # The same test, but without connect/disconnect
  157. #
  158. print "Testing select 1 row from table\n";
  159.  
  160. $dbh = $server->connect();
  161. $loop_time=new Benchmark;
  162.  
  163. for ($i=0 ; $i<$opt_loop_count ; $i++)
  164. {
  165.   $sth = $dbh->do("select * from bench1") # Select * from table with 1 record
  166.     or die $DBI::errstr;
  167. }
  168.  
  169. $end_time=new Benchmark;
  170. print "Time to select ($opt_loop_count): " .
  171.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  172.  
  173. #
  174. # The same test, but with 2 rows.
  175. #
  176. print "Testing select 2 rows from table\n";
  177.  
  178. $sth = $dbh->do("insert into bench1 values(2,200,'BBB')")
  179.   or die $DBI::errstr;
  180.  
  181. $loop_time=new Benchmark;
  182.  
  183. for ($i=0 ; $i<$opt_loop_count ; $i++)
  184. {
  185.   $sth = $dbh->do("select * from bench1") # Select * from table with 2 record
  186.     or die $DBI::errstr;
  187. }
  188.  
  189. $end_time=new Benchmark;
  190. print "Time to select ($opt_loop_count): " .
  191.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  192.  
  193. $sth = $dbh->do("drop table bench1")
  194.   or die $DBI::errstr;
  195.  
  196. if ($opt_fast && $opt_server eq "pg")
  197. {
  198.   $server->vacuum($dbh);
  199. }
  200.  
  201. ################################# PART:5 ###################################
  202. #### We'll create one table with a single blob field,but with a
  203. #### huge record in it and then we'll do $opt_loop_count selects
  204. #### from it.
  205.  
  206. print "Testing retrieval of big records ($str_length bytes)\n";
  207.  
  208. do_many($dbh,$server->create("bench1", ["b blob"], []));
  209. $dbh->{LongReadLen}= $str_length; # Set retrieval buffer
  210.  
  211. my $string=(A) x ($str_length); # This will make a string $str_length long.
  212. $sth = $dbh->prepare("insert into bench1 values(?)") or die $dbh->errstr;
  213. $sth->execute($string) or die $sth->errstr;
  214. undef($string);
  215. if ($opt_fast && $opt_server eq "pg")
  216. {
  217.   $server->vacuum($dbh);
  218. }
  219.  
  220. $loop_time=new Benchmark;
  221.  
  222. for ($i=0 ; $i < $opt_loop_count ; $i++)
  223. {
  224.   $sth = $dbh->prepare("select * from bench1");
  225.   if (!$sth->execute || !(@row = $sth->fetchrow_array) ||
  226.       length($row[0]) != $str_length)
  227.   {
  228.     warn "$DBI::errstr - ".length($row[0])." - $str_length **\n";
  229.   }
  230.   $sth->finish;
  231. }
  232.  
  233. $end_time=new Benchmark;
  234. print "Time to select_big ($opt_loop_count): " .
  235.   timestr(timediff($end_time, $loop_time),"all") . "\n\n";
  236.  
  237. $sth = $dbh->do("drop table bench1")
  238.   or do {
  239.     if (not $dbh->errstr =~ /non-exclusive access/)
  240.         { die $dbh->errstr; }
  241.     };
  242.  
  243. if ($opt_fast && $opt_server eq "pg")
  244. {
  245.   $server->vacuum($dbh);
  246. }
  247.  
  248.  
  249. ################################ END ###################################
  250. ####
  251. #### End of the test...Finally print time used to execute the
  252. #### whole test.
  253.  
  254. $dbh->disconnect;
  255. end_benchmark($start_time);
  256.